package edu.zut.software.identity.utils;

import com.mysql.cj.log.Log;
import edu.zut.software.identity.constant.WorkWXAPI;
import edu.zut.software.identity.controller.GetAccessToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WXAPIUtil {
    private final static Logger logger = LoggerFactory.getLogger(WXAPIUtil.class);

    public static String accessTokenAuth = "access_token:auth";

    public static String accessTokenUser = "access_token:user";

    /**
     * 获取access token
     * @param key 根据此key获取
     * @return
     */
    public static Map<String, Object> getAccessToken(String key){
        String secret = "";
//        if(key == accessTokenAuth){
//            secret = WorkWXAPI.SECRET;
//        }else if(key == accessTokenUser){
//            secret = WorkWXAPI.USER_SECRET;
//        }
        secret=WorkWXAPI.SECRET;
        String json = HttpClientUtil.get(String.format(WorkWXAPI.GET_ACCESS_TOKEN_URL, WorkWXAPI.CORPID, secret));
        Map<String, Object> data = JsonUtil.WXJsonToMap(json);
        if (Integer.parseInt(data.get("errcode").toString()) == 0) {
            return  data;
        } else {
            logger.info("{} get access error,msg:{}", key, data.toString());
        }
        return null;
    }
    /**
     * 执行创建部门操作
     * @param name 部门名称
     * @param parentid 父部门id
     * @param  order 排序(不指定时传入0)
     * @return
     */
    public static Map<String, Object> createDepartment(String name, Integer parentid, Integer order){
        Map<String, Object> createMap = new HashMap<>();
        createMap.put("name", name);
        createMap.put("parentid", parentid);
        if(order > 0){
            createMap.put("order", order);
        }
//        return HttpClientUtil.wxRequest(accessTokenAuth, String.format(WorkWXAPI.DEPARTMENT_CREATE, accessTokenAuth),
//                JsonUtil.toJson(createMap), HttpClientUtil.METHOD_POST);
        return HttpClientUtil.wxRequest(accessTokenAuth, String.format(WorkWXAPI.DEPARTMENT_CREATE, GetAccessToken.getAccessToken()),
                JsonUtil.toJson(createMap), HttpClientUtil.METHOD_POST);
    }
    /**
     * 创建用户信息
     * @param json
     * @return
     */
    public static Map<String, Object> createContact(String json){
        return HttpClientUtil.wxRequest(accessTokenAuth, String.format(WorkWXAPI.CONTACTS_CREATE, GetAccessToken.getAccessToken())
                , json, HttpClientUtil.METHOD_POST);
    }

    public static Map<Integer, Map<String, Object>> getDepartmentList(){
        Map<Integer, Map<String, Object>> departmentMap = new HashMap<>();
        Map<String, Object> jsonMap = HttpClientUtil.wxRequest(GetAccessToken.getAccessToken(), String.format(WorkWXAPI.DEPARTMENT_LIST,
                GetAccessToken.getAccessToken()), null, HttpClientUtil.METHOD_GET);
        System.out.println(jsonMap);
        if(Integer.parseInt(jsonMap.get("errcode").toString()) == 0){
            List<Map<String, Object>> list = (List<Map<String, Object>>) jsonMap.get("department");

            for(Map<String, Object> map : list){
                departmentMap.put(Integer.parseInt(map.get("id").toString()), map);
            }
        }
        return departmentMap;
    }
    public static Map<String,Object> deleteDepartment(String id){
        return HttpClientUtil.wxRequest(GetAccessToken.getAccessToken(),String.format(WorkWXAPI.DEPARTMENT_DELETE,GetAccessToken.getAccessToken(),id),null,HttpClientUtil.METHOD_GET);

    }
//    public static Map<String,Object> updatePerson(String id)
    public static Map<String,Object>deletePerson(String id){
        return HttpClientUtil.wxRequest(GetAccessToken.getAccessToken(),String.format(WorkWXAPI.PERSON_DELETE,GetAccessToken.getAccessToken(),id),null,HttpClientUtil.METHOD_GET);
    }
    public static Map<String, Object>updateDepartment(String id,String name){
        Map<String, Object> map=new HashMap<>();
        map.put("id",id);
        map.put("name",name);
        return HttpClientUtil.wxRequest(accessTokenAuth, String.format(WorkWXAPI.DEPEARTMENT_UPDATE, GetAccessToken.getAccessToken()),
                JsonUtil.toJson(map), HttpClientUtil.METHOD_POST);
    }
}
